<template>
  <div class="header">
    <div class="left">
      <h3>叩丁狼电商运营平台</h3>
      <el-icon :size="22" @click="emit('clickMenu')"><Menu /></el-icon>
    </div>
    <div class="right">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          {{ store.username
          }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
        </span>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="index">首页</el-dropdown-item>
            <el-dropdown-item command="logout">退出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from "element-plus";
import { ArrowDown, Menu } from "@element-plus/icons-vue";
import { useRouter } from "vue-router";
import { logout } from "@/common";
import { userLogoutApi } from "@/http/api";
import { useUserStore } from "@/store/User";
const router = useRouter();
const store = useUserStore();
const handleCommand = (command: string | number | object) => {
  if (command === "index") {
    router.push("/index");
  } else if (command === "logout") {
    logout(
      () => {
        userLogoutApi();
      },
      () => {
        ElMessage.success("退出成功");
      }
    );
  }
};

const emit = defineEmits(["clickMenu"]);
</script>

<style scoped lang="less">
.header {
  height: 70px;
  width: 100%;
  display: flex;
  background-color: #409eff;
  align-items: center;
  justify-content: space-between;
  .left {
    h3 {
      font-size: 24px;
      padding: 0 20px;
      color: #fff;
      font-weight: 400;
    }
    display: flex;
    align-items: center;
    :deep(.el-icon) {
      margin-top: 5px;
      color: #fff;
    }
  }
  .right {
    :deep(.el-dropdown-link) {
      color: #fff;
    }
  }
}
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
</style>
